package org.jeecg.modules.AccessoriesCenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.AccessoriesCenter.entity.DilinniDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.DilinniEntity;
import org.jeecg.modules.AccessoriesCenter.mapper.ButtonManageMapper;
import org.jeecg.modules.AccessoriesCenter.mapper.DilinniManageMapper;
import org.jeecg.modules.AccessoriesCenter.service.DilinniManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DilinniManageServiceImpl implements DilinniManageService {


    @Autowired
    ButtonManageMapper buttonManageMapper;

    @Autowired
    DilinniManageMapper mapper;
    @Override
    public IPage<DilinniEntity> getDilinniList(Page<DilinniEntity> page, Integer status, String dilinniNumber) {
        return mapper.getDilinniList(page,status, dilinniNumber);
    }

    @Override
    public int updateDilinnistatus(int id, int status) {
        return mapper.updateDilinnistatus(id,status);
    }

    @Override
    public DilinniEntity getDilinnibyId(int id) {
        return mapper.getDilinnibyId(id);
    }

    @Override
    public int addDilinni(DilinniEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addDilinni(entity);
    }

    /**
     * 查看编号是否已经存在
     * @param dilinniNumber
     * @return
     */
    @Override
    public String getDilinniNumberbyDilinniNumber(String dilinniNumber) {
        return mapper.getDilinniNumberbyDilinniNumber(dilinniNumber);
    }

    @Override
    public int updateDilinni(DilinniEntity entity) {
        return mapper.updateDilinni(entity);
    }

    @Override
    public void piliangUpdatestatus(List<Integer> ids, int status) {
        for(int i=0;i<ids.size();i++){
            mapper.updateDilinnistatus(ids.get(i),status);
        }
    }

    @Override
    public List<DilinniDaochuEntity> getDilinniDaochulist(Integer status, String dilinniNumber) {
        return mapper.getDilinniDaochulist(status,dilinniNumber);
    }

    @Override
    public int importExcel(List<DilinniDaochuEntity> list) {


        if(list!=null){
            for( int i=0;i<list.size();i++){
                list.get(i).setCreateTime(new Date());

                String oldcunzai=getDilinniNumberbyDilinniNumber( list.get(i).getDilinniNumber());
                //如果不存在 则执行插入操作
                if(oldcunzai==null){
                    String sysuserId=buttonManageMapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                    list.get(i).setSysuserId(sysuserId);
                    if(sysuserId!=null)
                    mapper.addExcelDilinni(list.get(i));
                }else {
                    String sysuserId=buttonManageMapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                    list.get(i).setSysuserId(sysuserId);
                    mapper.updateExcelDilinni(list.get(i));
                }
            }
        }
        return 1;
    }
}
